/**
 * Predefined variables
 * Name = toast
 * DateTime = Sun Jan 16 2022 19:23:02 GMT+0800 (中国标准时间)
 * Author = Created by zhj9922
 * FileBasename = toast.ts
 * gitee： https://gitee.com/coyeking
 */
 
import { _decorator, Component, Node, Label,Color,tween,UIOpacity,Vec3} from 'cc';
import { ModeluType } from '../../define/ccenum';
const { ccclass, property } = _decorator;
let v3_targetPos = new Vec3(0, 150, 0);

@ccclass('toast')
export class toast extends Component {
    @property({
        type: Label,
        displayName:'文本',
    })
    private label: Label = null;
    
    @property(UIOpacity)
    public UIOpacityBg: UIOpacity = null;
	
	
    public init(msg:string,type:ModeluType){
        var self = this;
        this.label.string = msg;
        let color: Color = Color.WHITE;

        if (type == ModeluType.SUCCESS){
            color = Color.GREEN;
        }
        
        if (type == ModeluType.FAIL){
            color = Color.RED;
        }

        this.label.color = color;


        setTimeout(function() {
            if (self.node){
                tween(self.node)
                .to(0.5, { position: v3_targetPos} , { easing: 'smooth' })
                .start();
                
                self.UIOpacityBg.opacity = 255;
                tween(self.UIOpacityBg)
                .to(0.5, { opacity: 0} , { easing: 'smooth' })
                .call(()=>{
                    self.node.removeFromParent();
                })
                .start();
            }
        }, 1000);
    }
}

